package entities;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;

import sound.SoundManager;
import util.Vector2D;

public class Shot extends Entity {

	protected int damage;
	protected Entity owner;

	public Shot(String image, Entity owner, int damage, Vector2D velocity)
			throws SlickException {
		super(image);
		this.owner = owner;
		this.maxHealth = 1;
		score = 5;

		this.health = maxHealth;
		this.damage = damage;
		this.velocity = velocity;
		alive = true;
	}

	public float getPower() {
		return damage;
	}

	@Override
	public String getName() {
		return "Shot";
	}

	@Override
	public void checkEntityPosition(GameContainer gc) {
		if (pos.getX() >= (gc.getWidth()) || pos.getX() <= 0
				|| pos.getY() >= gc.getHeight() || pos.getY() <= 0) {
			level.removeEntity(this);
		}

	}

	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
		// System.out.println(isAlive());
		for (Entity e : level.getCollisions(this)) {
			if (e != owner && !(e instanceof Shot) && !(e instanceof Enemy && owner instanceof Enemy)) {
				System.out.printf("Shot from %s hit %s!\n", owner.getName(),
						e.getName());
				e.takeDamage(damage);
				if (!e.isAlive()) {
//					System.out.println(e.getScore());
					level.getPlayer().addScore(e.getScore());
					if (!(e instanceof Shot)) {
						if (e == level.getPlayer()) {
							SoundManager.getSoundManager().playSound(SoundManager.playerExplosion, 1.5f);
						} else {
							SoundManager.getSoundManager().playSound(SoundManager.getSoundManager().getExplosionSound(), 2.5f);
						}
						
					}
				} else {
					SoundManager.getSoundManager().playSound(SoundManager.getSoundManager().getBulletSound(), 1f);
				}
				level.removeEntity(this);
			}
		}
	}
}
